package com.darren.euc.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.darren.euc.dao.ClientDao;
import com.darren.euc.dao.RoomDao;
import com.darren.euc.entity.Client;
import com.darren.euc.entity.PageBean;
import com.darren.euc.entity.Room;
import com.darren.euc.service.RoomService;
import com.darren.euc.util.StringUtil;

import net.sf.json.JSONObject;


@Service
public class RoomServiceImpl implements RoomService {
	
	@Autowired
    private RoomDao roomDao;
	
	@Autowired
    private ClientDao clientDao;

    @Override
    public Room save(Room room) {
        return roomDao.save(room);
    }

    @Override
    public Room update(Room room) {
        return roomDao.update(room);
    }
    
    
    @Override
    public void delete(Room room) {
    	roomDao.delete(room);
    }
    @Override
    public Room findRooById(Integer roomId){
    	return roomDao.findById(roomId);
    }
    
    @Override
    public List<Room> findRooByMemberId(Integer memberId){
    	return roomDao.findRooByMemberId(memberId);
    }
    
    @Override
    public List<Room> findRooByMemberIdWithoutTimeLimit(Integer memberId){
    	return roomDao.findRooByMemberIdWithoutTimeLimit(memberId);
    }
    
    @Override
    public List<Room> findRoomBySpaceId(Integer spaceId){
    	return roomDao.findRoomBySpaceId(spaceId);
    }
    
    
	@Override
	public List<Room> getAll() {
		return roomDao.getAll();
	}
	
	public List<Room> getRoomByClientAndSpaceId(Integer clientId,Integer spaceId){		
		return roomDao.getRoomByClientAndSpaceId(clientId,spaceId);
	}
	
	@SuppressWarnings("rawtypes")
	public PageBean queryForPageUseHQL(JSONObject requestJson,Integer pageSize, Integer currentPage){		
		String HQL = null;
		String likes = null;
		String equals = null;
			List<Object> parameters = new ArrayList<Object>();
		JSONObject likeJson = requestJson.getJSONObject("like");
		Iterator likeIterator = likeJson.keys();
		while(likeIterator.hasNext()){
			String key = (String) likeIterator.next();
			key = StringUtil.convertNullToEmptyString(key);
			String value = likeJson.getString(key);
			value = StringUtil.convertNullToEmptyString(value);
			if(!"".equals(value)){
				likes = (likes == null?"": likes + " OR ") +  key + " like ? ";
				parameters.add("%"+value+"%");
			}
		}
		HQL = likes == null?null:(" (" + likes + ") ");		
		JSONObject equalsJson = requestJson.getJSONObject("equals");
        Iterator equalsIterator = equalsJson.keys();        
		while(equalsIterator.hasNext()){
			String key = (String) equalsIterator.next();
			key = StringUtil.convertNullToEmptyString(key);
			String value = equalsJson.getString(key);
			value = StringUtil.convertNullToEmptyString(value);
			if(!"".equals(value)){
				equals = (equals == null?"": ""+equals + " and ") +  key + " = ? ";
				if("space.id".equals(key)){
					parameters.add(Integer.parseInt(value));
				}else if("enable".equals(key)){
					parameters.add(Boolean.parseBoolean(value));
				}else{
					parameters.add(value);
				}
				
			}
		}		
		if(HQL != null){
			if(equals != null){
				HQL = HQL + " and " + equals;
			}
		}else{
			HQL = equals;
		}
		HQL = " from Room " + (HQL == null?"":" where " + HQL);
		return roomDao.queryForPageByHQL(HQL, pageSize, currentPage,parameters.toArray());
	}
	
	public List<Room> queryAvaliableRoom(Integer spaceId,String roomName,String rentStart,String rentEnd){
		List<Room> rooms = roomDao.queryAvaliableRoom(spaceId, roomName, rentStart, rentEnd);
		return rooms;
	}
	
	public Map<String,Object> getUsedDetail(Integer roomId){
		Map<String,Object> usedDetail = new HashMap<String,Object>();
		List<Client> clients = clientDao.getClientByRoomId(roomId);
		usedDetail.put("client", clients);
		return usedDetail;
	}

}